module comp {
	/**
	 *
	 * @author 
	 *
	 */
	export class SoundPlayer {
        private soundList: Object = {};
        private loadurl: string = "";
        private times: number = 1;
        private static _instance: SoundPlayer;
        private ch: egret.SoundChannel;
        private nowSound: string = "";
        public static getInstance():SoundPlayer{
            if (SoundPlayer._instance==null){
                SoundPlayer._instance = new SoundPlayer();
            }
            return SoundPlayer._instance 
        }
		public constructor() {
		}
		/*****
		 * 播放声音
		 */ 
		public playSound(url:string,times:number=1):void{
            if(this.ch) {
                console.log("soundPlayer===========27====="+this.nowSound+":"+url);
                if(this.nowSound == url && this.nowSound !="piano_mp3"){
                    return;
                }
                this.ch.stop();
                this.ch.position = 0;
                this.ch.removeEventListener(egret.Event.SOUND_COMPLETE,this.soundComp,this);
                this.ch = null;
            }
            this.times = times;
            var sound: egret.Sound = RES.getRes(url);
            this.nowSound = url;
            this.ch = sound.play(0,times);
            this.ch.addEventListener(egret.Event.SOUND_COMPLETE,this.soundComp,this);
//            if (this.soundList.hasOwnProperty(url)){
//
//                var sound: egret.Sound = this.soundList[url];
//                //var channel: egret.SoundChannel = sound.play(0,times);
//                this.ch = sound.play(0,times);
//                this.ch.addEventListener(egret.Event.SOUND_COMPLETE,this.soundComp,this);
//            }else{
//                //创建 URLLoader 对象
//                var loader:egret.URLLoader = new egret.URLLoader();
//                //设置加载方式为声音
//                loader.dataFormat = egret.URLLoaderDataFormat.SOUND;
//                //添加加载完成侦听
//                loader.addEventListener(egret.Event.COMPLETE, this.onLoadComplete, this);       
//                var request:egret.URLRequest = new egret.URLRequest(url);
//                this.loadurl = url;
//                //开始加载
//                loader.load(request);
//                
//            }
		}
		
        private onLoadComplete(event:egret.Event):void {
            var loader:egret.URLLoader = <egret.URLLoader>event.target;    
            //获取加载到的 Sound 对象
            var sound:egret.Sound = <egret.Sound>loader.data;
            this.soundList[this.loadurl] = sound;
            if (this.times<1){
                this.times == 1;
            }
           // var channel: egret.SoundChannel = sound.play(0,this.times);
            this.ch = sound.play(0,this.times);
            this.ch.addEventListener(egret.Event.SOUND_COMPLETE,this.soundComp,this);
        }
        
        public soundComp(e:egret.Event):void{
            this.times=0;
            this.ch.stop();
            this.ch = null;
            this.nowSound = "";
//            if (this.times>0){
//                var s: egret.Sound = e.currentTarget;
//                s.play(false);
//            }
        }
        
	}
}
